﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class StaffController : Controller
    {
        IStaffService _staffService;

        public StaffController()
        {
            _staffService = new StaffService();
        }


        public ActionResult Index(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        public ActionResult List(int id)
        {
            ViewBag.Id = id;
            var staffs = _staffService.FindList(10, "", 0, id, 1);
            List<StaffViewModel> svms = new List<StaffViewModel>();
            foreach (var staff in staffs)
            {
                svms.Add(new StaffViewModel()
                {
                    Id = staff.Id,
                    Avatar = staff.Avatar,
                    Birthday = staff.Birthday,
                    Cuisine = staff.Cuisine,
                    Description = staff.Description,
                    Job = staff.Job,
                    Name = staff.Name,
                    Restaurant_Id = staff.Restaurant_Id,
                    Salary = staff.Salary,
                    Sex = staff.Sex
                });
            }
            return View(svms);
        }

        public ActionResult Add(int id)
        {
            ViewBag.Id = id;
            return View(new StaffViewModel() { Restaurant_Id = id });
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Add(StaffViewModel svm, int id)
        {
            ViewBag.Id = id;
            svm.Cuisine = Request["Cuisine"];

            if (ModelState.IsValid)
            {
                Staff staff = _staffService.Add(svm.Staff);

                if (staff != null && staff.Id > 0)
                {
                    return RedirectToAction("List", "Staff", new { Area = "RRShop", Id = id });
                }
                else
                {
                    ModelState.AddModelError("Name", "添加员工失败");
                    return View(svm);
                }
            }

            return Redirect("/RRShop/Staff/List/" + ViewBag.Id);
        }

        [Authorize]
        public ActionResult Edit(int id, int restaurant_id)
        {
            ViewBag.Id = restaurant_id;
            Staff staff = _staffService.Find(id);
            if (staff == null || staff.Id <= 0)
            {
                return RedirectToAction("Error404", "Home", new { Area = "Admin", returnUrl = "../../RRShop/Staff/List/" + restaurant_id });
            }
            else
            {
                StaffViewModel svm = StaffViewModel.FromStaff(staff);
                svm.Jobs.Find(s => s.Value == staff.Job.ToString()).Selected = true;
                return View(svm);
            }
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(StaffViewModel svm, int id, int restaurant_id)
        {
            if (ModelState.IsValid)
            {
                svm.Cuisine = Request["Cuisine"];
                Staff staff = svm.Staff;

                if (_staffService.Update(staff))
                {
                    return RedirectToAction("List", "Staff", new { Area = "RRShop", Id = svm.Restaurant_Id });
                }
                else 
                {
                    ModelState.AddModelError("Name", "添加员工失败");
                    return View(svm);
                }
            }

            return View(svm);
        }

        public ActionResult Delete(int id, int restaurant_id)
        {
            if (Request.IsAuthenticated)
            {
                Staff staff = _staffService.Find(id);
                _staffService.Delete(staff);
            }
            else
            {
                return RedirectToAction("Login", "Admin", new { Area = "RRShop", returnUrl = "/RRShop/Staff/List/" + restaurant_id });
            }

            return RedirectToAction("List", "Staff", new { Area = "RRShop", Id = restaurant_id });
        }
    }
}